package com.powerunion.datacollection.quartz;

import org.apache.commons.lang.StringUtils;

import com.powerunion.datacollection.common.Constants;

public class CronExpConversion {

	/**
	 * 设置转为UNIX cron expressions 转换算法
	 * 
	 * @param subType
	 *            报送周期类型
	 * @param commonNeeds
	 *            包括 小时 分钟 秒
	 * @param time
	 *            具体的某个月的几号 或是 某周的第几星期等等
	 * @param exactlyTime
	 *            具体的时间
	 * @return
	 */
	public static String convertDateToCronExp(int subType,
			String[] commonNeeds, String time, String exactlyTime) {
		StringBuffer cronEx = new StringBuffer();
		String commons = commonNeeds[0] + " " + commonNeeds[1] + " "
				+ commonNeeds[2]+" ";

		if (subType == Constants.TASK_PERIODS_TYPE_MONTHLY) {
			// 每月的某天某时间触发
			cronEx.append(commons).append(" ").append(time).append("*").append(
					" ").append("?");
		} else if (subType == Constants.TASK_PERIODS_TYPE_WEEKLY) {
			cronEx.append(commons).append(
					CronExRelated.specialCharacters.get(CronExRelated._ANY))
					.append(" ").append(
							CronExRelated.specialCharacters
									.get(CronExRelated._EVERY)).append(" ")
					.append(time).append(" ");
		} else if (subType == Constants.TASK_PERIODS_TYPE_DAYLY) {
			cronEx.append(commons).append(" ").append("*").append(" ").append(
					"*").append(" ").append("?").append(" ");
		} else if (subType == Constants.TASK_PERIODS_TYPE_NOTREPEATE) {// 不重复触发，根据某个具体时间触发
			String[] times = StringUtils.split(exactlyTime, "T");
			String[] dayOfMonth = StringUtils.split(times[0], "-");
			String[] HMS = StringUtils.split(times[1], ":");

			cronEx.append(HMS[2]).append(" ").append(HMS[1]).append(" ")
					.append(HMS[0]).append(" ").append(dayOfMonth[2]).append(
							" ").append(dayOfMonth[1]).append(" ").append(
							CronExRelated.specialCharacters
									.get(CronExRelated._ANY)).append(" ");
		}
		return cronEx.toString().trim();
	}

	public static String convertDateToCronExp(String exactlyTime) {
		StringBuffer cronEx = new StringBuffer();
		// 不重复触发，根据某个具体时间触发
		String[] times = StringUtils.split(exactlyTime, "T");
		String[] dayOfMonth = StringUtils.split(times[0], "-");
		String[] HMS = StringUtils.split(times[1], ":");

		cronEx.append(HMS[2]).append(" ").append(HMS[1]).append(" ").append(
				HMS[0]).append(" ").append(dayOfMonth[2]).append(" ").append(
				dayOfMonth[1]).append(" ").append(
				CronExRelated.specialCharacters.get(CronExRelated._ANY))
				.append(" ");

		return cronEx.toString().trim();
	}

	public static void main(String[] args) {
		String[] commonNeeds = { "30", "10", "4" };
		// String[] monthlyNeeds = {"30","10","4"};
		// String[] commonNeeds = {"30","10","4"};
		// String[] commonNeeds = {"30","10","4"};
		String times = "2010-3-13T21:31:21";
		int everyWhat = 0;
		// String results = CronExpConversion.convertDateToCronExp(" userDefined
		// ", commonNeeds, null, null, times);
		String results = CronExpConversion.convertDateToCronExp(everyWhat,
				commonNeeds, "15", times);

		System.out.println("results:" + results);
	}

}
